package cn.cjp.m3d26;

import java.util.ArrayList;
import java.util.Collection;

public class eCollection {
    public static void main(String[] args) {
        //使用多态创建集合对象
        //换成其他集合也可  Collection<String> coll = new HashSet           <>();
        Collection<String> coll = new ArrayList<>();

        //把对象添加到当前集合，返回值一般是true,故可不接受
        coll.add("hhh");
        coll.add("aaa");
        coll.add("bbb");
        System.out.println(coll);
        System.out.println("==================");

        //把给定的对象在集合中删除，若成功删除就返回true，否则返回false
        System.out.println(coll.remove("aaa"));
        System.out.println(coll.remove("ccc"));
        System.out.println(coll);
        System.out.println("==================");

        //判断集合是否包含给定的对象
        System.out.println(coll.contains("aaa"));
        System.out.println(coll.contains("bbb"));
        System.out.println("==================");

        //判断集合是否为空
        System.out.println(coll.isEmpty());
        System.out.println("==================");

        //集合对象个数
        System.out.println("对象个数"+coll.size());
        System.out.println("==================");

        //集合元素存储到数组
        Object[] arr = coll.toArray();
        for (int i = 0; i <arr.length ; i++) {
            System.out.println(arr[i]);
        }
        System.out.println("==================");

        //清空集合元素，集合还存在
        coll.clear();
        System.out.println(coll.isEmpty());
    }
}
